package org.market.domain.strategy.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.market.types.common.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/7 下午2:52
 * @Version: 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {
    /**策略id*/
    private Long strategyId;
    /**奖品id*/
    private Integer awardId;
    /**规则类型[1-抽奖规则,2-策略规则]*/
    private Integer ruleType;
    /**规则模型[rule_lock]*/
    private String ruleModel;
    /**规则值*/
    private String ruleValue;
    /**规则描述*/
    private String ruleDesc;

    /**
     *获取权重值
     * 例如:4000:102,103,104,105 5000:102,103,104,105,106,107 6000:102,103,104,105,106,107,108,109
     * @return
     */
    public Map<String, ArrayList<Integer>> getRuleWeightValue() {
        if (!"rule_weight".equals(ruleModel)) return null;
        //按空格分割值
        String[] ruleValues = ruleValue.split(Constants.SPACE);
        Map<String, ArrayList<Integer>> map = new HashMap<>();
        for (String value : ruleValues) {
            if (value == null || value.isEmpty()) return null;

            //按分号分割成键值对
            String[] split = value.split(Constants.COLON);
            if (split.length != 2) {
                throw new IllegalArgumentException("rule_weight invalid input format:"+ ruleValues);
            }

            ArrayList<Integer> weightsConfigList = new ArrayList<>();
            String[] weightsConfig = split[1].split(Constants.SPLIT);
            for (String weight : weightsConfig) {
                weightsConfigList.add(Integer.parseInt(weight));
            }
            map.put(value, weightsConfigList);
        }
        return map;
    }
}
